package com.gitee.huxing.course.conc.conc03.tool;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

/**
 * @Description:
 * @ProjectName: JavaCourse
 * @Package: com.gitee.huxing.course.conc.conc03.tool
 * @ClassName: CountDownLatchDemo
 * @Author: huxingmale
 * @DateTime: 2021-08-25 23:58
 */
public class CountDownLatchDemo1 {

    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(5);
        for (int i = 0; i < 5; i++) {
            new Thread(new ReadNum(i, countDownLatch)).start();
        }
        countDownLatch.await();
        System.out.println("===>各个子线程执行完成");
        System.out.println("===>主线程执行结束");
    }

    static class ReadNum implements Runnable{

        private int id;

        private CountDownLatch latch;

        public ReadNum(int id, CountDownLatch latch){
            this.id = id;
            this.latch = latch;
        }

        @Override
        public void run() {
            synchronized (this){
                System.out.println("id:" + id + "," + Thread.currentThread().getName());
                System.out.println("线程组任务" + id + "结束，其他任务继续");
                latch.countDown();
            }

        }
    }
}
